<template>
  <zeda-dialog
    v-model="visible"
    :title="(id <= 0 ? '新增图纸' : '编辑图纸') + ''"
    width="500px"
    :before-close="handleClose"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <form-button
      slot="footer"
      :loading="loading"
      @cancel="handleClose"
      @submit="submit"
    >
    </form-button>
  </zeda-dialog>
</template>

<script>
import { detail, add, edit } from "@/api/park/bluePrint";
import { getparkList } from "@/api/park/parkmsg.js";
import FormButton from "@/components/FormButton";

export default {
  components: {
    FormButton,
  },
  props: {
    id: {
      type: [Number, String],
      default: "",
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      parkList: [],
      loading: false, // 按钮加载中判断
      areaProps: {
        value: "areaId",
        children: "children",
        label: "name",
        checkStrictly: false,
        expandTrigger: "hover",
      },
      apsForm: {
        formRef: "form",
        formData: {},
        formConfig: {
          formAttr: {
            // inline: true,
            "label-position": "right",
            size: "small",
            bsColumn: {
              col: 1,
              middle: true,
            },
            rules: {
              name: [
                {
                  required: true,
                  message: "请输入图纸名称",
                  trigger: "blur",
                },
              ],
              parkId: [
                {
                  required: true,
                  message: "请选择园区",
                  trigger: ["blur", "change"],
                },
              ],
              paperUrl: [
                {
                  required: true,
                  message: "请上传图纸",
                  trigger: ["blur", "change"],
                },
              ],
            },
          },
          properties: {
            parkId: {
              label: "园区",
              render: (h) => {
                let that = this;
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().parkId;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择园区",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("parkId", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      that.parkList.map((item) => {
                        let { name, id } = item;
                        return h("el-option", {
                          props: {
                            label: name,
                            value: id,
                            key: id,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            name: {
              label: "图纸名称",
              typeName: "el-input",
              placeholder: "请输入图纸名称",
            },
            paperUrl: {
              label: "上传图纸",
              typeName: "ImageUpload",
              limit: 1,
              fileSize: 5,
              accept: "image/*",
              fileBigness: "大于400*400px，小于1000*1000px",
            },
            remarks: {
              label: "备注",
              typeName: "el-input",
              type: "textarea",
              rows: 4,
              placeholder: "请输入备注",
            },
          },
        },
      },
    };
  },
  watch: {
    visible: {
      handler(value) {
        if (value) {
          this.getParkList();
          if (String(this.id).length > 0) {
            this.getInfo();
          }
        }
      },
      deep: false,
      immediate: false,
    },
  },
  methods: {
    getParkList() {
      getparkList()
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.parkList = res?.rows ?? [];
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!错误码401001");
        });
    },
    getInfo() {
      detail(this.id)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let newData = res?.data || {};
          newData.areaId = res?.data?.douDistrictId;
          this.$refs?.form?.setFieldsValue(newData);
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员! 错误码307004");
        });
    },
    submit() {
      const data = {
        ...this.$refs.form.getFieldsValue(),
      };
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.id) {
            this.editRequest(data);
          } else {
            this.addRequest(data);
          }
        } else {
          return false;
        }
      });
    },
    addRequest(data) {
      add(data)
        .then((res) => {
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.$message.success("操作成功");
          this.handleClose(true);
        })
        .catch(() => {
          this.loading = false;
          // this.$message.error("系统错误,请联系管理员!错误码307002");
        });
    },
    editRequest(data) {
      edit(data)
        .then((res) => {
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.$message.success("操作成功");
          this.handleClose(true);
        })
        .catch(() => {
          this.loading = false;
          // this.$message.error("系统错误,请联系管理员!错误码307003");
        });
    },
    handleClose(status) {
      this.$refs["form"].resetFields();
      this.$emit("update:visible", false);
      this.$emit("close", status === true ? status : false);
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-form {
  .el-input,
  .el-cascader,
  .el-input-number,
  .el-select {
    width: 100% !important;
  }
  .map {
    display: flex;
    .el-input {
      margin-right: 10px;
    }
  }
}
</style>
